package com.gy.smart.life.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gy.smart.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：gy on 17/1/10
 * 说明：
 */
public class LifeTitleView extends LinearLayout {

    private TextView tvAll,tv1,tv2,tv3,tv4,tv5,tv6,tv7,tv8;
    private List<TextView> tvs;
    private LifeTitleSelect listener;
    private List<String> titles;

    public LifeTitleView(Context context) {
        this(context, null);
    }

    public LifeTitleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LifeTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        tvs = new ArrayList<>();
        View view = View.inflate(context, R.layout.view_life_title,this);
        tvAll = (TextView) view.findViewById(R.id.view_life_title_tv_all);
        tv1 = (TextView) view.findViewById(R.id.view_life_title_tv1);
        tv2 = (TextView) view.findViewById(R.id.view_life_title_tv2);
        tv3 = (TextView) view.findViewById(R.id.view_life_title_tv3);
        tv4 = (TextView) view.findViewById(R.id.view_life_title_tv4);
        tv5 = (TextView) view.findViewById(R.id.view_life_title_tv5);
        tv6 = (TextView) view.findViewById(R.id.view_life_title_tv6);
        tv7 = (TextView) view.findViewById(R.id.view_life_title_tv7);
        tv8 = (TextView) view.findViewById(R.id.view_life_title_tv8);
        tvs.add(tvAll);
        tvs.add(tv1);
        tvs.add(tv2);
        tvs.add(tv3);
        tvs.add(tv4);
        tvs.add(tv5);
        tvs.add(tv6);
        tvs.add(tv7);
        tvs.add(tv8);

        for (int i = 0; i < tvs.size(); i++) {
            final int finalI = i;
            tvs.get(i).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.lifeTitleSelect(finalI);
                    setSelect(finalI);

                }
            });
        }
    }

    private void setSelect(int posi){
        for (int i = 0; i < tvs.size(); i++) {
            if(posi == i){
                tvs.get(i).setBackgroundResource(R.drawable.shape_life_title_bg);
            } else {
                tvs.get(i).setBackgroundResource(R.drawable.shape_life_title_normal_bg);
            }
        }

    }

    public void setTitles(List<String> titles){
        this.titles = titles;
        if(titles.size() > 0){
            for (int i = 1; i < tvs.size(); i++) {
                tvs.get(i).setText(titles.get(i - 1));
            }
        }
    }

    public void setListener(LifeTitleSelect listener){
        this.listener = listener;
    }

    public interface LifeTitleSelect{
        void lifeTitleSelect(int position);
    }
}
